<?php
include('../../../../inc/application_settings.php');

$email = $_COOKIE['EMAIL'];
$target = (isset($_REQUEST['T'])) ? $_REQUEST['T'] : null;
$password = Cryptic::decrypt($_COOKIE['PASSWORD'], $email);
$auth = new Authentication($email, $password);
$response = array();

if ($auth->checkAuthentication()){
	switch ($target) {
		case 'NEW':
			$response = createNewGallery();
			break;
		case 'EDIT':
			$response = updateGallery();
			break;
		case 'IMAGE':
			$response = updateImage();
			break;
	}
}

echo json_encode($response);


/**
 * creates a new gallery entry in bio.gallery
 */
function createNewGallery(){
	$response = array('SUCCESS' => 1, 'SUCCESS_MSG' => 'Galerie erfolgreich angelegt!', 'GALLERY_ID' => null);

	try{
	$sql = 'insert into ' . DB_BIO . '.gallery (active) values (?)';
	$params = array(ACTIVE);
	$sqlObj = Sql_Object::getInstance();
	$sqlObj->connect();
	$sqlObj->beginTransaction();
	$sqlObj->execute($sql, $params);
	$galleryId =  $sqlObj->getLastRecordId();
	
	$langObj = new Language($sqlObj);
	setName($langObj, $sqlObj, $galleryId, APPLICATION_ID);

	$sqlObj->endTransaction();
	$sqlObj->disconnect();
	
	$response['GALLERY_ID'] = $galleryId;
	} catch (Exception $e) {
		$response['SUCCESS'] = 0;
		$response['ERROR_MSG'] = $e->getMessage();
	}
	return $response;
}


function updateGallery(){
	$response = array('SUCCESS' => 1, 'SUCCESS_MSG' => 'Galerie erfolgreich upgedated!', 'GALLERY_ID' => null);
	$name_en = (isset($_REQUEST['GALLERY_NAME_EN'])) ? $_REQUEST['GALLERY_NAME_EN'] : null;
	$name_de = (isset($_REQUEST['GALLERY_NAME_DE'])) ? $_REQUEST['GALLERY_NAME_DE'] : null;
	$galleryId = (isset($_REQUEST['GALLERY_ID'])) ? $_REQUEST['GALLERY_ID'] : null;
	$active = (isset($_REQUEST['GALLERY_ACTIVE_DE'])) ? $_REQUEST['GALLERY_ACTIVE_DE'] : ACTIVE;
	
	$response['GALLERY_ID'] = $galleryId;
	
	if ($galleryId != null){
		$sql = 'select name_de_id, name_en_id from ' . DB_BIO . '.gallery_view where gallery_id = ?';
		$params = array($galleryId);
		
		try{
			$sqlObj = Sql_Object::getInstance();
			$sqlObj->connect();
			$langObj = new Language($sqlObj);
			$sqlObj->beginTransaction();
			$sqlObj->execute($sql, $params);
				if (($row = $sqlObj->fetch()) != null){
					$name_de_id = $row['name_de_id'];
					$name_en_id = $row['name_en_id'];
					$langObj->updateValue($name_de, $name_de_id);
					$langObj->updateValue($name_en, $name_en_id);
				}
				
			$sql = 'update ' . DB_BIO . '.gallery set active = ? where gallery_id = ?';
			$params = array($active, $galleryId);
			$sqlObj->execute($sql, $params);	
			$sqlObj->endTransaction();
			$sqlObj->disconnect();
		} catch (Exception $e){
			$response['SUCCESS'] = 0;
			$response['ERROR_MSG'] = $e->getMessage();
		}
	}

	return $response;
}


/**
 * sets the name and active values for given image
 */
function updateImage(){
	$response = array('SUCCESS' => 1, 'SUCCESS_MSG' => 'Image erfolgreich upgedated!', 'IMAGE_ID' => null);
	$name_en = (isset($_REQUEST['IMAGE_NAME_EN'])) ? $_REQUEST['IMAGE_NAME_EN'] : null;
	$name_de = (isset($_REQUEST['IMAGE_NAME_DE'])) ? $_REQUEST['IMAGE_NAME_DE'] : null;
	$imageId = (isset($_REQUEST['IMAGE_ID'])) ? $_REQUEST['IMAGE_ID'] : null;
	$active = (isset($_REQUEST['IMAGE_ACTIVE_DE'])) ? $_REQUEST['IMAGE_ACTIVE_DE'] : ACTIVE;
	$width = (isset($_REQUEST['IMAGE_WIDTH_DE'])) ? $_REQUEST['IMAGE_WIDTH_DE'] : null;
	$height = (isset($_REQUEST['IMAGE_HEIGHT_DE'])) ? $_REQUEST['IMAGE_HEIGHT_DE'] : null;
	
	$response['IMAGE_ID'] = $imageId;
	
	if ($imageId != null){
		$sql = 'select name_de_id, name_en_id from ' . DB_BIO . '.gallery_image_view where image_id = ?';
		$params = array($imageId);
		
		try{
			$sqlObj = Sql_Object::getInstance();
			$sqlObj->connect();
			$langObj = new Language($sqlObj);
			$sqlObj->beginTransaction();
			$sqlObj->execute($sql, $params);
				if (($row = $sqlObj->fetch()) != null){
					$name_de_id = $row['name_de_id'];
					$name_en_id = $row['name_en_id'];
					$langObj->updateValue($name_de, $name_de_id);
					$langObj->updateValue($name_en, $name_en_id);
				}
				
			$sql = 'update ' . DB_BIO . '.gallery_image set active = ?, width = ?, height = ? where image_id = ?';
			$params = array($active, $width, $height, $imageId);
			$sqlObj->execute($sql, $params);	
			$sqlObj->endTransaction();
			$sqlObj->disconnect();
		} catch (Exception $e){
			$response['SUCCESS'] = 0;
			$response['ERROR_MSG'] = $e->getMessage();
		}
	}
	return $response;
}




/**
 * registrates a gallery language key for name
 * @param 		mixed		 $langObj
 * @param 		mixed		 $sqlObj
 * @param 		int			 $galleryId
 * @param 		int			 $applicationId
 */
function setName($langObj, $sqlObj, $galleryId, $applicationId){
	$name_en = (isset($_REQUEST['GALLERY_NAME_EN'])) ? $_REQUEST['GALLERY_NAME_EN'] : null;
	$name_de = (isset($_REQUEST['GALLERY_NAME_DE'])) ? $_REQUEST['GALLERY_NAME_DE'] : null;
	
	$languageKeyId = $langObj->registrateKey('gallery_' . $galleryId . '_name', $applicationId);
	setAttribute('name', $name_de, $galleryId, $sqlObj, $applicationId, $langObj, $languageKeyId, GERMAN);
	setAttribute('name', $name_en, $galleryId, $sqlObj, $applicationId, $langObj, $languageKeyId, ENGLISH);
}


/**
 * registrates a gallery language value for given language and set entry in gallery table to langugage key
 * @param 		string		$column
 * @param 		string		$value
 * @param 		int			$galleryId
 * @param 		mixed		$sqlObj
 * @param 		int			$applicationId
 * @param 		mixed		$langObj
 * @param 		int			$languageKeyId
 * @param 		string		$language
 */
function setAttribute($column, $value, $galleryId, $sqlObj, $applicationId, $langObj, $languageKeyId, $language){
	$langObj->registrateValue($value, $languageKeyId, $language);
	
	$sql = 'update ' . DB_BIO . '.gallery set ' . $column . '_id = ? where gallery_id = ?';
	$params = array($languageKeyId, $galleryId);
	$sqlObj->execute($sql, $params);
}




?>